import NameImgAdItem from "./Items/NameImgAdItem";
import { ui } from "../../../../ui/layaMaxUI";
import { BaseAdView } from "./BaseAdView";
import { AdConfigData, MoveDir } from "./ShiyouAdManager";
import TimeTool from "../../Tool/TimeTool";


export default class PopularrecommendView extends ui.ShiyouAdView.PopularrecommendViewUI implements BaseAdView 
{
    private m_moveDir:MoveDir=MoveDir.None;
    private m_moveSpeed:number=67;
    private m_moveMaxValue:number=1;
    

    constructor() {
        super();
        Laya.stage.addChild(this);
        this.m_AdList.on(Laya.Event.MOUSE_DOWN, this,this.OnMouseDown);
        this.m_AdList.on(Laya.Event.MOUSE_UP, this, this.OnMouseUp);
        this.m_AdList.on(Laya.Event.MOUSE_OUT, this, this.OnMouseUp);
        this.m_closeBtn.on(Laya.Event.CLICK, this, this.Hide);
    }


    createChildren() {
        super.createChildren();
        this.m_AdList.renderHandler = new Laya.Handler(this, this.OnRenderHandler);
        this.m_AdList.elasticEnabled = true;
        this.m_AdList.selectEnable = true;
        this.m_AdList.repeatX = 3;
        this.m_AdList.vScrollBarSkin = "";
    }


    public OnRenderHandler(cell: NameImgAdItem, index: number) {
        let skindata = cell.dataSource;
        cell.SetData(skindata,"Type4",skindata.Type4);
    }


    update()
    {
        if(this.m_AdList.length<=9){
            return;
        }
        switch (this.m_moveDir) {
            case MoveDir.Down:
                this.MoveDown();
                break;
            case MoveDir.Up:
                this.MoveUp();
                break;
            default:
                break;
        }
    }

    OnMouseDown(){
        this.m_moveDir=MoveDir.None;
    }


    OnMouseUp(){
        let scrobarvalue=this.m_AdList.scrollBar.value;
        if (scrobarvalue>this.m_moveMaxValue/2) {
            this.m_moveDir=MoveDir.Down;
        }
        else{
            this.m_moveDir=MoveDir.Up;
        }
    }

    private MoveDown(){

        if (this.m_AdList.scrollBar.value< this.m_AdList.scrollBar.max) {
            this.m_AdList.scrollBar.value+=this.m_moveSpeed*TimeTool.DeltaTime;
        }
        else{
            this.m_moveDir=MoveDir.Up;
        }
    }
   
 
    private MoveUp()
    {
        if (this.m_AdList.scrollBar.value> this.m_AdList.scrollBar.min) {
            this.m_AdList.scrollBar.value-=this.m_moveSpeed*TimeTool.DeltaTime;
        }
        else{
            this.m_moveDir=MoveDir.Down;
        }
    }

    InitData(gameadsData: Array<AdConfigData>) {
        if (gameadsData.length < 1) return;

        let data = [];
        for (let index = 0; index < gameadsData.length; index++) {
            const element = gameadsData[index];
            if (element.Type4 !=null)
            {
                data.push(element);
            }   
        }
        if (data.length < 1) return;
        
        data.sort(this.Sort.bind(this));
        this.m_AdList.array = data;
        this.m_moveMaxValue= this.m_AdList.scrollBar.max;
        this.m_moveDir=MoveDir.Down;
    }


    public Show() 
    {
        this.visible = true; 
        Laya.timer.frameLoop(2,this, this.update);
    }


    public Hide() {
        this.visible = false;
        Laya.timer.clearAll(this);
    }


    Sort(a,b){
        if (a.Type4 < b.Type4)return -1;
        if (a.Type4 == b.Type4)return 0;   
        return 1;
    }

}